﻿using ElsCircuit.Model;
using Newtonsoft.Json;
using System.IO;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ElsCircuit
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        List<circuit> circuits = new List<circuit>();
        List<circuitModel> SaveData = new List<circuitModel>();
        public MainWindow()
        {
            InitializeComponent();
            this.Loaded += MainWindow_Loaded;
        }

        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
        {
            int index = 1;
            for (int i = 0; i < 8; i++)
            {
                for (Int32 j = 0; j < 8; j++)
                {
                    if (i == 3 && j == 3)
                    {
                        continue;
                    }
                    if (i == 4 && j == 3)
                    {
                        continue;
                    }
                    if (i == 3 && j == 4)
                    {
                        continue;
                    }
                    if (i == 4 && j == 4)
                    {
                        continue;
                    }
                    circuit circuit = new circuit();
                    circuit.mode.index = index++;
                    
                    view.Children.Add(circuit);
                    Grid.SetColumn(circuit, i);
                    Grid.SetRow(circuit, j);
                    circuits.Add(circuit);
                }
            }
            LoadLog();
            foreach (var circuit in circuits)
            {
                circuit.mode.DataChange += Mode_DataChange;
            }
        }

        private void Mode_DataChange()
        {
            SaveLog();
        }

        string file = "userdata";
        private void LoadLog()
        {
            if (!File.Exists(file))
            {
                return;
            }
            var data = File.ReadAllText(file);
            try
            {
                SaveData = JsonConvert.DeserializeObject<List<circuitModel>>(data);
            }
            catch
            {

            }
            if (SaveData != null && SaveData.Count > 0)
            {
                foreach (var circuitModel in SaveData)
                {
                    var cir = circuits.Find(x => x.mode.index == circuitModel.index);
                    if (cir!=null)
                    {
                        cir.mode.CircuitType = circuitModel.CircuitType;
                        cir.mode.colorType = circuitModel.colorType;
                        cir.mode.IsCopy = circuitModel.IsCopy;
                        cir.mode.IsNeedBuy = circuitModel.IsNeedBuy;
                        cir.mode.Draw();
                    }
                }
            }
        }
        /// <summary>
        /// 
        /// </summary>
        private void SaveLog()
        {
            if (!File.Exists(file))
            {
                File.Create(file).Close();
            }
            SaveData.Clear();
            circuits.ForEach(x => {
                if (x.mode.CircuitType!=EnumCircuitType.non)
                {
                    SaveData.Add(x.mode);
                }
            });
            File.WriteAllText(file, Newtonsoft.Json.JsonConvert.SerializeObject(SaveData));
        }
        private void RadioButton_Click(object sender, RoutedEventArgs e)
        {
            int colorType = -1;
            RadioButton radioButton = sender as RadioButton;
            if (radioButton.Content.ToString() == "红" && radioButton.IsChecked == true)
            {
                colorType = 0;
            }
            else if (radioButton.Content.ToString() == "蓝" && radioButton.IsChecked == true)
            {
                colorType = 1;
            }
            else if (radioButton.IsChecked == true)
            {
                colorType = 2;
            }
            if (colorType > -1)
            {
                foreach (var circuit in circuits)
                {
                    circuit.mode.colorType = colorType;
                    circuit.mode.Draw();
                }
            }
            SaveLog();
        }
    }
}